﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace content
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow:Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void SetExpanded(bool isExpanded)
        {
            foreach(var v in stackPanelLeft.Children)
                if(v is Expander expander)
                    expander.IsExpanded=isExpanded;
        }


        private Button lastButton=null;

        private void Button_Click(object sender,RoutedEventArgs e)
        {
            Button btn=e.Source as Button;
            if(lastButton!=null)
                lastButton.Foreground=Brushes.Blue;
            if(btn.Content.ToString()=="默认页")
                frame1.Source=new Uri("DefaultPage.xaml",UriKind.Relative);
            else if(btn.Content.ToString()=="全部展开")
                SetExpanded(true);
            else if(btn.Content.ToString()=="全部折叠")
                SetExpanded(false);
            else
            {
                btn.Foreground=Brushes.Red;
                Uri uri=new Uri(btn.Tag.ToString(),UriKind.Relative);
                try
                {
                    object obj=Application.LoadComponent(uri);
                    if(obj is Window)
                    {
                        frame1.Source=null;
                        var w=obj as Window;
                        w.WindowStartupLocation=WindowStartupLocation.CenterScreen;
                        w.ShowDialog();
                    }
                    else if(obj is Page)
                    {
                        if(lastButton==btn)
                            frame1.Refresh();
                        else
                            frame1.Source=uri;
                    }
                    else
                    {
                        MessageBox.Show("不支持此类型，注意目标必须是Window或者Page。","出错了",MessageBoxButton.OK,MessageBoxImage.Error);
                    }
                }
                catch
                {
                    stackPanelLeft.Cursor=frame1.Cursor=Cursors.Arrow;
                    MessageBox.Show("未找到 "+uri.OriginalString,"出错了",MessageBoxButton.OK,MessageBoxImage.Error);
                }
            }
            lastButton=btn;
        }
    }
}